// pages/classify/classify.js
const db = wx.cloud.database();
const classifyList = db.collection("classifyList");
const classifyRouge = db.collection("classifyRouge");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    db: [],
    classifyList:[],
    classifyListItem:[],
    tapDatabaseName:"allCategory00",
    dbname: "classifyRouge",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(this.data.activeKey)
    classifyList.get().then(res=>{
      console.log(res.data[0].titles)
      this.setData({
        classifyList:res.data,
        classifyListItem:res.data[0].titles
      })
    })
    classifyRouge.get().then(res => {
      console.log(res.data)
      this.setData({
        db: res.data
      })
    })
  },

  onChange(event) {
    wx.showToast({
      icon: 'none',
      title: `切换至第${event.detail}项`
    });
    this.setData({ 
      activeKey: event.detail,
      tapDatabaseName: this.data.classifyList[event.detail].TapDataBaseName,
      classifyListItem:this.data.classifyList[event.detail].titles,
      dbname: this.data.classifyList[event.detail].showDataBaseName
    })

    console.log(this.data.tapDatabaseName);
    const dataBaseName = db.collection(this.data.dbname);
    dataBaseName.get().then(res => {
      this.setData({
        db: res.data
      })
    });
  },
  onMoreTap:function(event){
    var category = event.currentTarget.dataset.category;
    console.log(category);
    wx.navigateTo({
      url: 'category/category?category=' + category +'&tapDatabaseName='+this.data.tapDatabaseName
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})